package org.squabble.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.squabble.dao.AccountDAO;
import org.squabble.domain.Account;


@Service
public class CustomUserDetailService implements UserDetailsService {

	@Autowired
	private AccountDAO accountDAO;

	private final Log log = LogFactory.getLog(this.getClass());

	@Override
	public UserDetails loadUserByUsername(String email)
			throws UsernameNotFoundException, DataAccessException {
		// TODO Auto-generated method stub
		Account account = accountDAO.findBy("email", email);
		if (account == null) {
			log.debug("Unable to find user: "+email);
			throw new UsernameNotFoundException("User not found", email);			
		}
		return account;
	}

}
